<?php

class Admin_AdvertisementController extends Zend_Controller_Action{
	public $_model = null;

	public function init(){
		$this->_model = new Application_Model_DbTable_Advertisement();

	}

	public function indexAction(){
		$filters = array();

		$id = $this->_request->getParam('id','');
		if($id!=''){
			$filters['id'] = $id;
		}
		
		//từ khóa
		$keyword = $this->_request->getParam('keyword','');
		$filters['keyword']=$keyword;
		$this->view->keyword = $keyword;

		// từ ngày
		$fromDate = $this->_request->getParam('fromDate',null);
		//$filters['fromDate']=Cms_String_String::getIntFromDate($fromDate);
		$filters['fromDate']=Cms_String_String::getDateFromString($fromDate);
		$this->view->fromDate = $fromDate;

		// đến ngày
		$toDate = $this->_request->getParam('toDate',null);
		$filters['toDate']=Cms_String_String::getDateFromString($toDate,'last');//chuyển về định dạng của mysql
		$this->view->toDate = $toDate;

		// trạng thái | status
		$status = $this->_request->getParam('searchStatus',"");
		$filters['status']=$status;
		$this->view->status=$status;

		// sắp xếp | order
		$order = $this->_request->getParam('order',"");
		if($order!=""){
			list($orderBy,$orderType) = explode('-', $order);
		}else{
			$orderBy=null;
			$orderType=null;
		}


		//get item count per page
		$icpp = $this->_request->getParam('icpp',20);

		//get current page number
		$currentPage = $this->_request->getParam('page',1);

		$list = $this->_model->getList($filters,$orderType,$orderBy);

		// pagination
		$paginator = Zend_Paginator::factory($list);
		$paginator->setCurrentPageNumber($currentPage);
		$paginator->setItemCountPerPage($icpp);
		$paginator->setPageRange(5);

		//assign to view
		$this->view->paginator=$paginator;

	}

	public function insertAction(){
		$form = new Admin_Form_Advertisement();
		if($this->_request->isPost()){
			if($form->isValid($_POST)){
				$row = $this->_model->createRow($_POST);
				$row->advertisement_alias = Cms_String_String::toAlias($row->advertisement_name);
				if($form->file->receive() && $form->getValue('file')!=""){						
					$dir = PUBLIC_PATH.'/uploads/images/advertisements';
					
					$cms_image = new Cms_Image_PhpThumb($dir.'/tmp/'.$form->getValue('file'),$row->advertisement_alias);
					
					$filename = $cms_image->toResize(array(
							1=>array('width'=>'1000','height'=>'350','dir'=>$dir.'/'),
					));
					
					$row->advertisement_image = $filename;
					
					//xóa hình ảnh gốc
					@unlink($dir.'/tmp/'.$filename);
				}
				$row->save();
				$this->_helper->flashMessenger('Thêm thành công!');
				$this->_helper->redirector('index','advertisement','admin');
			}
		}
		$this->view->form = $form;
	}

	public function updateAction(){
		$form = new Admin_Form_Advertisement();
		$id = $this->_request->getParam('id');
		$row  = $this->_model->find($id)->current();
		
		if($this->_request->isPost()){
			if($form->isValid($_POST)){

				$row->advertisement_name = $form->getValue('advertisement_name');
				$row->advertisement_alias = Cms_String_String::toAlias($form->getValue('advertisement_name'));
				$row->advertisement_link = $form->getValue('advertisement_link');
				$row->advertisement_status = $form->getValue('advertisement_status');
				$row->advertisement_advertisement_category_id = $form->getValue('advertisement_advertisement_category_id');
				
				if($form->file->receive() && $form->getValue('file')!=""){
					
					$dir = PUBLIC_PATH.'/uploads/images/advertisements';
					$cms_image = new Cms_Image_PhpThumb($dir.'/tmp/'.$form->getValue('file'),$row->advertisement_alias);
					$filename = $cms_image->toResize(array(
							1=>array('width'=>'1000','height'=>'350','dir'=>$dir),
					));
					
					//xóa hình ảnh đã lưu
					@unlink($dir.'/'.$row->advertisement_image);
					
					//lưu hình ảnh mới
					$row->advertisement_image = $filename;
					
					//xóa hình ảnh gốc
					@unlink($dir.'/tmp/'.$filename);
				}
				
				$row->save();
				$this->_helper->flashMessenger('Cập nhật thành công!');
				$this->_helper->redirector('index','advertisement','admin');
			}
		}
		$form->populate($row->toArray());
		$this->view->form = $form;
	}

	public function deleteAction(){
		
		$status = 0;
		$id = @(array)$this->_request->getParam('id');

		$rows = $this->_model->fetchAll($this->_model->select()->where('advertisement_id IN (?)',$id));
		if(count($rows)>0){
			foreach($rows as $row){
				$dir = PUBLIC_PATH.'/uploads/images/advertisements';
				@unlink($dir.'/'.$row->advertisement_image);
				$row->delete();
				$status = 1;
			}
			$this->_helper->flashMessenger('Xóa dữ liệu thành công!');
			$this->_helper->redirector('index','advertisement','admin');
		}
		
	}
	
	public function changestatusAction(){
		$status ="";
		$id = $this->_request->getParam('id');
	
		$row = $this->_model->find($id)->current();
	
		if(!$row){
			$status['error']="sản phẩm không tồn tại";
		}else{
			$row->advertisement_status =(int)(1 - $row->advertisement_status);
			$status['status']=$row->advertisement_status;
			try {
				$row->save();
			} catch (Exception $e) {
				$status['errors']="lỗi {$e->getMessage()}";
			}
				
		}
		$this->_helper->viewRenderer->setNoRender(true);
		$this->_helper->layout()->disableLayout();
		$this->_helper->json->sendJson($status);
	}
}

